package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.R;
import com.hongxun.design.bean.ArticleFootBean;
import com.hongxun.design.bean.FootInvolBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringCodeTableUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/6.
 */
public class FootAdapter extends BaseAdapter {
    List<ArticleFootBean> beans;
    Context context;
    LayoutInflater inflater;

    public FootAdapter(Context context, List<ArticleFootBean> beans) {
        this.beans = beans;
        this.context = context;
        inflater = LayoutInflater.from(context);
        projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int i) {
        return beans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ArticleFootBean bean = (ArticleFootBean) getItem(i);
        Hollder hollder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_foot_article, null);
            hollder = new Hollder(view);
            view.setTag(hollder);
        } else {
            hollder = (Hollder) view.getTag();
        }

        try {
            hollder.foot_month.setText(bean.getMonth());
            hollder.invol_words.setText("参与阶段工作" + bean.getInvol().size() + "个");
            hollder.finish_words.setText("完成阶段工作" + bean.getFinish().size() + "个");
            hollder.publish_words.setText("发布文章" + bean.getPublish().size() + "篇");

//            if (hollder.InvolData.size() == 0 || hollder.InvolData.size() != bean.getInvol().size()) {
//                hollder.InvolData.clear();
//                hollder.InvolData.addAll(bean.getInvol());
            if (bean.getInvol() == null || bean.getInvol().size() == 0) {
                hollder.invollist.removeAllViews();
                hollder.invollist.setVisibility(View.GONE);
                hollder.invol_words.setVisibility(View.GONE);
                hollder.invol_line.setVisibility(View.GONE);
            } else {
                hollder.invollist.setVisibility(View.VISIBLE);
                hollder.invol_words.setVisibility(View.VISIBLE);
                hollder.invol_line.setVisibility(View.VISIBLE);
                addInvollist(bean.getInvol(), hollder.invollist);
//                    if (hollder.involAdapter == null) {
//                        hollder.involAdapter = new InvolAndFinishAdapter(context, hollder.InvolData);
//                        hollder.invollist.setAdapter(hollder.involAdapter);
//                    } else {
//                        hollder.involAdapter.notifyDataSetChanged();
//                    }
            }

//            }
//            if (hollder.FinishData.size() == 0 || hollder.FinishData.size() != bean.getFinish().size()) {
//                hollder.FinishData.clear();
//                hollder.FinishData.addAll(bean.getFinish());

                if (bean.getFinish()==null||bean.getFinish().size() == 0) {
                    hollder.invollist.removeAllViews();
                    hollder.finishlist.setVisibility(View.GONE);
                    hollder.finish_words.setVisibility(View.GONE);
                    hollder.finish_line.setVisibility(View.GONE);
                } else {
                    hollder.finishlist.setVisibility(View.VISIBLE);
                    hollder.finish_words.setVisibility(View.VISIBLE);
                    hollder.finish_line.setVisibility(View.VISIBLE);
                    addInvollist(bean.getFinish(), hollder.finishlist);
//                    if (hollder.finishAdapter == null) {
//                        hollder.finishAdapter = new InvolAndFinishAdapter(context, hollder.FinishData);
//                        hollder.finishlist.setAdapter(hollder.finishAdapter);
//                    } else {
//                        hollder.finishAdapter.notifyDataSetChanged();
//                    }
                }
//            }
//            if (hollder.PublishData.size() == 0 || hollder.PublishData.size() != bean.getPublish().size()) {
//                hollder.PublishData.clear();
//                hollder.PublishData.addAll(bean.getPublish());

                if (bean.getPublish()==null||bean.getPublish().size() == 0) {
                    hollder.invollist.removeAllViews();
                    hollder.publishlist.setVisibility(View.GONE);
                    hollder.publish_words.setVisibility(View.GONE);
                    hollder.publish_line.setVisibility(View.GONE);
                } else {
                    hollder.publishlist.setVisibility(View.VISIBLE);
                    hollder.publish_words.setVisibility(View.VISIBLE);
                    hollder.publish_line.setVisibility(View.VISIBLE);
                    addInvollist(bean.getPublish(), hollder.publishlist);
//                    if (hollder.publishAdapter == null) {
//                        hollder.publishAdapter = new InvolAndFinishAdapter(context, hollder.PublishData);
//                        hollder.publishlist.setAdapter(hollder.publishAdapter);
//                    } else {
//                        hollder.publishAdapter.notifyDataSetChanged();
//                    }
                }
//            }


        } catch (Exception e) {
        }

        return view;
    }

    private void addInvollist(List<FootInvolBean> data, LinearLayout layout) {
        layout.removeAllViews();
       if (data==null||data.size()==0)return;
        for (FootInvolBean bean:data){
            addItem(bean,layout);
        }
    }

    private void addItem(FootInvolBean bean, LinearLayout layout){
        View view =inflater.inflate(R.layout.listview_item_footinvol, null);

        SimpleDraweeView   image = (SimpleDraweeView) view.findViewById(R.id.footinvol_image);
        TextView name = (TextView) view.findViewById(R.id.footinvol_name);
        TextView type = (TextView) view.findViewById(R.id.footinvol_type);
        TextView time = (TextView) view.findViewById(R.id.footinvol_time);
        TextView work = (TextView) view.findViewById(R.id.footinvol_work);
        try {
             time.setText(bean.create_time);
            if (bean.in_work!=null){
                name.setText(bean.p_name);
                 image.setImageURI(UriUtil.getUri(bean.p_thumb));
                 work.setVisibility(View.VISIBLE);
                if (bean.p_type!=null&&bean.p_type.length()>0){
                    type.setText(getProjectType(bean.p_type)+"项目");
                    type.setVisibility(View.VISIBLE);
                }else{
                     type.setVisibility(View.GONE);
                }

                work.setText("参与工作："+bean.in_work);

            }else{
                 name.setText(bean.title);
                image.setImageURI(UriUtil.getUri(bean.article_thumb));
                 type.setVisibility(View.GONE);
                work.setVisibility(View.GONE);
            }

        } catch (Exception e) {
        }

        layout.addView(view);
    }
    List<SystemTypeBean> projectType;
    private String getProjectType(String code) {
        if (projectType == null)
            projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
        for (SystemTypeBean s : projectType) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }

    class Hollder {
        LinearLayout invollist, finishlist, publishlist;
        TextView foot_month, invol_words, finish_words, publish_words;
        //        InvolAndFinishAdapter involAdapter;
//        InvolAndFinishAdapter finishAdapter;
//        InvolAndFinishAdapter publishAdapter;
//        List<FootInvolBean> InvolData;
//        List<FootInvolBean> FinishData;
//        List<FootInvolBean> PublishData;
        View invol_line, finish_line, publish_line;

        Hollder(View view) {
//            InvolData = new ArrayList<>();
//            FinishData = new ArrayList<>();
//            PublishData = new ArrayList<>();
            foot_month = (TextView) view.findViewById(R.id.foot_month);
            invol_words = (TextView) view.findViewById(R.id.foot_invol_words);
            finish_words = (TextView) view.findViewById(R.id.foot_finish_words);
            publish_words = (TextView) view.findViewById(R.id.foot_publish_words);
            invollist = (LinearLayout) view.findViewById(R.id.foot_invol_list);
            finishlist = (LinearLayout) view.findViewById(R.id.foot_finish_list);
            publishlist = (LinearLayout) view.findViewById(R.id.foot_publish_list);
            invol_line = view.findViewById(R.id.foot_invol_line);
            finish_line = view.findViewById(R.id.foot_finish_line);
            publish_line = view.findViewById(R.id.foot_publish_line);

        }
    }


}
